package com.tekj.cxqcb.view.Module.fragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.SelectAllForBusinessBean;
import com.tekj.cxqcb.view.Module.activity.Order.NewOrdersActivity;
import com.tekj.cxqcb.view.Module.adapter.BuyingIntentionAdapter;
import com.tekj.cxqcb.view.Module.decoration.SpacesItemDecoration;
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration;

import butterknife.BindView;
import common.base.fragment.BaseFragment;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;

/**
 * 购车咨询fragment
 */
public class BuyingIntentionFragment extends BaseFragment {

    @BindView(R.id.rv_buying)
    RecyclerView rvBuying;
    @BindView(R.id.srl_main)
    SwipeRefreshLayout srlMain;
    private BuyingIntentionAdapter mAdapter;
    private int type;
    private BusinessDao businessDao;
    private int page = 1;
    private String shopName;

    @Override
    public void init() {
        type = getArguments().getInt("type");
        businessDao = new BusinessDao(activity, this);

        mAdapter = new BuyingIntentionAdapter(null);
        rvBuying.setLayoutManager(new LinearLayoutManager(activity));
        rvBuying.setAdapter(mAdapter);
        rvBuying.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.ghostwhite)));

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        }, rvBuying);

        srlMain.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_gz:
                        //TODO 关注
                        businessDao.UpdateByFollow(
                                mAdapter.getData().get(position).getId(),
                                mAdapter.getData().get(position).getIsFollow() == 0 ? 1 : 0
                        );
                        mAdapter.getData().get(position).setIsFollow(
                                mAdapter.getData().get(position).getIsFollow() == 0 ? 1 : 0);

                        mAdapter.notifyItemChanged(position);

                        break;

                    case R.id.tv_type:
                        //TODO 创建订单
//                        new WebCoreAction(activity, Urls.Domain_H5 + "/add-intention"
//                                + "?userName=" + mAdapter.getData().get(position).getNickname()
//                                + "&userPhone=" + mAdapter.getData().get(position).getPhone()
//                                + "&userId=" + mAdapter.getData().get(position).getUserId()
//                                + "&idcard=" + mAdapter.getData().get(position).getIdcard()
//                                + "&id=" + mAdapter.getData().get(position).getId()
//                                + "&uid=" + MainBActivity.User.getUserId()
//                        );
                        startActivity(new Intent(activity, NewOrdersActivity.class)
                                .putExtra("id", mAdapter.getData().get(position).getId())
                                .putExtra("userid", mAdapter.getData().get(position).getUserId())
                                .putExtra("shopname", shopName));
                        break;

                    case R.id.tv_phone:
                        //TODO 拨打电话
                        Tools.CallPhone(activity, mAdapter.getData().get(position).getPhone());
                        break;
                }
            }
        });
        rvBuying.addItemDecoration(new SpacesItemDecoration(10));
        getData();
    }

    void getData() {
        businessDao.SelectAllForBusiness(MainBActivity.User.getUserId(), type, page);
    }

    @Override
    public int setLayout() {
        return R.layout.fragment_buying_intention;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 购车意向列表:
                SelectAllForBusinessBean businessBean = (SelectAllForBusinessBean) bindingViewBean.getBean();
                if (businessBean.getCode().equals("0000000")) {
                    shopName=businessBean.getData().getShopName();
                    if (page == 1) {
                        mAdapter.setNewData(businessBean.getData().getList());
                    } else {
                        mAdapter.addData(businessBean.getData().getList());
                    }
                    mAdapter.loadMoreComplete();
                    if (businessBean.getData().getList().size() < 10) {
                        mAdapter.loadMoreEnd();
                    }

                }
                if (srlMain != null) {
                    srlMain.setRefreshing(false);
                }
                break;
        }
    }


}
